<template>
  <div>
    <!-- 资讯分类 -->
    <!-- <h3>资讯分类</h3> -->
    <!-- 新增按钮 -->
    <el-button type="primary" plain size="small" @click="addHandler"
      >新增</el-button
    >
    <!-- 表格开始 -->
    <el-table :data="categorys" style="width: 100%">
      {{ categorys }}
      <el-table-column label="序号" align="center" type="index">
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="no" label="排序号" align="center">
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- slot-scope是自定义插槽 scope.row是获取需要编辑的一整行数据 -->
          <el-button
            type="primary"
            plain
            size="small"
            @click="editHandler(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" plain size="small" @click="deleteHandler(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <!-- 嵌入验证表单开始 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="栏目名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="no">
          <el-input v-model="ruleForm.no"></el-input>
        </el-form-item>
        <el-form-item label="栏目介绍" prop="description">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <!-- 嵌入验证表单结束 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
    
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      
      dialogFormVisible: false,
     
      title: "添加资讯信息",
      ruleForm: {},
      rules: {
        name: [
          { required: true, message: "请输入栏目名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions("consultSort", ["findAllConsult", "saveConsult","deleteConsult"]),

    // 添加资讯信息
    addHandler() {
      this.dialogFormVisible = true;
    },
    // 提交并且保存添加栏目信息
    submitHandler() {
      this.saveConsult(this.ruleForm).then((res) => {
        console.log(res);
        if (res.status === 200) {
          this.dialogFormVisible = false;
          this.findAllConsult(this.list);
          this.$message({
            type: "success",
            message: res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    // 编辑并且保存栏目信息
    editHandler(data){
        this.ruleForm=data;
        this.dialogFormVisible=true;
        this.title="编辑栏目信息"
    },
    deleteHandler(id){
      // console.log(id);
      let params={
        id,
      }
      this.deleteConsult(params).then(res=>{
        console.log(res);
        if(res.status===200){
          this.findAllConsult(this.list);
          this.$message({
            type:'success',
            message:res.message
          })
        }else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      })
    },
   
  },
  computed: {
    ...mapState("consultSort", ["categorys"]),
  },
  created() {
    this.findAllConsult(this.list);
  },
};
</script>

<style scoped>
</style>